<?php
/**
 * Created by Boume.
 * Date: 4/29 0029
 * Time: 23:52
 */
namespace app\admin\controller;
use app\admin\model\Auth;
use think\Controller;

class Base extends Controller
{
    public function initialize()
    {
        parent::initialize();
        $this->inspect_extend();    //检测扩展开启情况
        $this->inspect_auth();  //检测权限,登录情况
    }
    //扩展检测
    private function inspect_extend()
    {
        $arr = [];
        $extensions = get_loaded_extensions();
        //判断是否开启了openssl
        if (in_array('openssl', $extensions) == false) $arr[] = '请开启openssl';
        if (!empty($arr)) {
            echo $this->fetch('/inspect', [
                'arr' => $arr,
            ]);
            die();
        }
    }

    //权限检测
    private function inspect_auth()
    {
        $m = strtolower($this->request->module());
        $c = strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . '_' . "$2", $this->request->controller()));
        $a = strtolower($this->request->action());
        //判断是否登录
        if (session('?U') == false) $this->redirect('passport/login');
        //如果是index控制器也跳过
        if ($m == 'admin' && $c == 'index') return true;
        if ($m == 'admin' && $c == 'fun') return true;
        $Node = new \app\admin\model\Node();
        $node = $Node->where([
            ['module', '=', $m],
            ['controller', '=', $c],
            ['action', '=', $a]
        ])->find();
        if (empty($node)) $this->error('节点不存在');

        //如果是创始人和超级管理员角色则跳过
        if (session('U.id') == 1 || session('U.role_id') == 1) return true;

        $Auth = new Auth();
        $auth = $Auth->where([
            ['rid', '=', session('U.role_id')],
            ['nid', '=', $node->id]
        ])->count();
        if ($auth == 0) $this->error('没有权限');
    }

    public function _empty()
    {
        die('error');
    }
}